home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / pcx2pxo.tproj / imfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-08  |  1.3 KB  |  65 lines

  1. #include  <stdio.h>
  2. #include  <libc.h>
  3. #include  <objc/objc.h>
  4. #include  "../common.h"
  5.  
  6. int optimalBits(unsigned char *pattern, int num)
  7. /* How many bits are needed to represent given patterns */
  8. {
  9.     int i, x;
  10.  
  11.     if (num > 16) return 8;
  12.     if (num == 1) { /* 1 bit; only one color */
  13.         if (pattern[0] || pattern[0xff]) return 1;
  14.     }else if (num == 2) { /* 1 bit */
  15.         if (pattern[0] && pattern[0xff]) return 1;
  16.     }
  17.     if (num <= 4) { /* 2 bits */
  18.         for (i = 1; i <= 0xfe; i++)
  19.             if (pattern[i] && (i != 0x55 && i != 0xaa))
  20.                 goto BIT4;
  21.         return 2;
  22.     }
  23. BIT4:    /* num <= 16 -- 4 bits */
  24.     for (i = 1; i <= 0xfe; i++)
  25.         if (pattern[i]
  26.             && ((x = i & 0x0f) != 0 && x != 0x0f && x != i >> 4))
  27.                 return 8;
  28.     return 4;
  29. }
  30.  
  31. int howManyBits(paltype *pal, int n)
  32. /* How many bits are needed to display colors of the palette ? */
  33. {
  34.     int i, c, num;
  35.     unsigned char *p, buf[256];
  36.  
  37.     for (i = 0; i < 256; i++) buf[i] = 0;
  38.     num = 0;
  39.     for (i = 0; i < n; i++) {
  40.         p = pal[i];
  41.         for (c = 0; c < 3; c++)
  42.         if (buf[p[c]] == 0) {
  43.             buf[p[c]] = 1;
  44.             if (++num > 16) return 8;
  45.         }
  46.     }
  47.     return optimalBits(buf, num);
  48. }
  49.  
  50. BOOL isGray(paltype *pal, int n)
  51. /* Is Gray-scaled all colors of the palette ? */
  52. {
  53.     int i;
  54.     unsigned char *p;
  55.  
  56.     if (pal == NULL)
  57.         return NO;
  58.     for (i = 0; i < n; i++) {
  59.         p = pal[i];
  60.         if (p[0] != p[1] || p[1] != p[2])
  61.             return NO;
  62.     }
  63.     return YES;
  64. }
  65.